package deliciouse.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import deliciouse.biz.UserBiz;
import deliciouse.biz.impl.UserBizImpl;
import deliciouse.entity.Users;

@WebServlet ("/CheckLogin")
public class LoginCheckServlet extends HttpServlet{

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		    	
    	String username =null;
    	String password = null;
    	if(request.getParameter("username")!=null && !request.getParameter("username").isEmpty())
    		username=request.getParameter("username");
    	
    	if(request.getParameter("password")!=null && !request.getParameter("password").isEmpty())
    		password=request.getParameter("password");
    	Users user = new Users();  	
    	user.setUserName(username);
    	user.setPassword(password);
    	UserBiz userbiz = new UserBizImpl();
    
    	//admin=user.getAdmin();
    	Users loginUser = userbiz.userCheckIn(user);
    	if(loginUser!=null){
    		loginUser.getIsAdmin();
    		int admin=userbiz.userCheckIn(user).getIsAdmin();
    		String testname = userbiz.userCheckIn(user).getUserName();
    		System.out.println(testname);
    		System.out.println(admin);
    		request.getSession().setAttribute("testname", testname);
    		request.getSession().setAttribute("admin", admin);
    		request.getSession().setAttribute("username", username);
    		response.sendRedirect("deliciousfoodindex");
    	}else{
    		request.getSession().setAttribute("username", null);
    		request.setAttribute("erro","用户密码有误！");
    		request.getRequestDispatcher("login.jsp").forward(request, response);
    	}
	}
}
